<template>
  <div class="login-view">
    <div class="content">
      <div class="content__item content__item-left"></div>
      <div class="content__item content__item-right">
        <div style="color: rgb(96, 99, 143); font-weight: bold; font-size: 24px">记账统计后台</div>
        <el-form ref="loginFormRef" :model="formData" :rules="rules" label-width="0">
          <el-form-item prop="loginCode">
            <el-input
              class="login-form-input"
              maxlength="11"
              clearable
              v-model="formData.loginCode"
              placeholder="输入账号"
            >
              <template #prepend>
                <el-icon size="22"> <User /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="pwd">
            <el-input
              class="login-form-input"
              clearable
              v-model="formData.pwd"
              placeholder="输入密码"
            >
              <template #prepend>
                <el-icon size="22"><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="captcha">
            <div style="display: flex; justify-content: center">
              <el-input
                class="login-form-input"
                clearable
                v-model="formData.captcha"
                placeholder="输入验证码"
              >
                <template #prepend>
                  <el-icon size="22"><Message /></el-icon>
                </template>
              </el-input>
              <el-image class="captcha-image" @click="getCaptcha" :src="captchaSrc" />
            </div>
          </el-form-item>
          <el-button
            size="large"
            class="login-button"
            @click="onLogin(loginFormRef)"
            :loading="loginButtonLoading"
          >
            登录
          </el-button>
        </el-form>
        <div class="xm-logo-wrapper">
          <el-image class="logo-img" :src="xm_logo" />
          <p class="logo-label">小马国炬</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import useUserStore from '@/stores/useUserStore'
import { Lock, Message, User } from '@element-plus/icons-vue'
import { captcha } from '@/api/system'
import { Md5 } from 'ts-md5'
import xm_logo from '@/assets/images/xm_logo.png'

interface IFormData {
  loginCode: string
  pwd: string
  captcha?: string
}

const router = useRouter()
const userStore = useUserStore()
// const enumStore = useEnumStore()
const formData = ref<IFormData>({
  loginCode: '13908899448',
  pwd: '1234567',
  captcha: ''
})

const rules = reactive<FormRules<IFormData>>({
  loginCode: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  pwd: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  captcha: [{ required: true, message: '请输入图片验证码', trigger: 'blur' }]
})

const loginButtonLoading = ref<boolean>(false)
const captchaSrc: any = ref()
const loginFormRef = ref()

async function onLogin(formEl: FormInstance | undefined) {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      loginButtonLoading.value = true
      const md5 = new Md5()
      const params: any = {
        captcha: formData.value.captcha,
        loginCode: formData.value.loginCode,
        pwd: md5.appendStr(formData.value.pwd).end()
      }

      const { msg, success } = await userStore.login(params)
      loginButtonLoading.value = false
      if (!success) {
        await getCaptcha()
        ElMessage.error(msg)
        return
      }

      await router.push({ path: '/', replace: true })
    } else {
      console.log('error submit!', fields)
    }
  })
}

async function getCaptcha() {
  const data: any = await captcha()
  captchaSrc.value = window.URL.createObjectURL(data)
}

async function keyDown(e: any) {
  //如果是回车则执行登录方法
  if (e.keyCode == 13) {
    await onLogin(loginFormRef.value)
  }
}

onMounted(() => {
  getCaptcha()
  window.addEventListener('keydown', keyDown)
})
</script>

<style scoped lang="scss">
.login-view {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  width: 100%;

  .content {
    display: flex;
    width: 900px;
    height: 500px;
    border-radius: 24px;
    background: rgb(232, 237, 243);
    box-shadow:
      rgba(50, 50, 93, 0.25) 0px 50px 100px -20px,
      rgba(0, 0, 0, 0.3) 0px 30px 60px -30px,
      rgba(10, 37, 64, 0.35) 0px -2px 6px 0px inset;

    &__item {
      width: 50%;
      height: 100%;
      border-radius: 24px;
    }

    &__item-left {
      background-image: url('@/assets/images/login_bg.png');
      background-repeat: no-repeat;
      background-position: 50%;
      background-size: 90%;
    }

    &__item-right {
      background: white;
      padding: 24px;
      box-sizing: border-box;
      position: relative;

      .login-form-input {
        margin: 12px 0;
        font-size: 18px;
        ::v-deep(.el-input__wrapper) {
          background: #f7f7f8;
          height: 44px;
        }
      }

      .captcha-image {
        width: 280px;
        height: 44px;
        border-radius: 6px 6px 6px 6px;
        opacity: 1;
        border: 1px solid #ededed;
        margin: 12px 0 0 12px;
      }

      .login-button {
        margin-top: 12px;
        width: 100%;
        height: 46px;
        background: #165dff;
        border-radius: 6px 6px 6px 6px;
        opacity: 1;
        color: #ffffff;
      }
    }

    .xm-logo-wrapper {
      position: absolute;
      width: 100%;
      bottom: 20px;
      display: flex;
      align-items: center;
      justify-content: center;

      & > .logo-img {
        width: 24px;
        height: 24px;
      }

      & > .logo-label {
        margin-left: 5px;
        font-size: 16px;
        font-weight: 600;
        color: #b4b4b4;
      }
    }
  }
}

@media (min-width: 768px) {
  .login-view {
    background-image: url('@/assets/svg/login.svg');
    background-repeat: no-repeat;
    background-position: 50%;
    background-size: 100%;
  }
}
</style>
